package JavaUtils.CSUtils;

import JavaUtils.CSUtils.Log.CLog;

import java.util.HashMap;
import java.util.Map;

public class CInt4 {

    static Map<Integer, Map<Integer, Map<Integer, Map<Integer, CInt4>>>> _Dic = new HashMap<>();
    static Map<Integer, CInt4> _idDic = new HashMap<>();
    static Map<String, CInt4> _nameDic = new HashMap<>();
    public static final CInt4 zero = new CInt4(new int[]{0, 0, 0, 0});
    public static final CInt4 minValue = new CInt4(new int[]{-256, 0, 0, 0});
    public static final CInt4 maxValue = new CInt4(new int[]{256, 32, 512, 511});
    int[] _array;

    public int[] array() {
        return _array;
    }

    /// <summary>
    /// a.范围限制(-256,256)
    /// </summary>
    public int a() {
        return _array[0];
    }

    /// <summary>
    /// b.范围限制(0,32)
    /// </summary>
    public int b() {
        return _array[1];
    }

    /// <summary>
    /// c.范围限制(0,512)
    /// </summary>
    public int c() {
        return _array[2];
    }

    /// <summary>
    /// d.范围限制(0,512)
    /// </summary>
    public int d() {
        return _array[3];
    }

    CInt4(int[] array) {
        _Init(array, false);
    }

    CInt4(int[] array, Boolean rangeIsError) {
        _Init(array, rangeIsError);
    }

    void _Init(int[] array, Boolean rangeIsError) {
        //不使用原始数组，避免原始数组被修改的错误
        _array = new int[]{array[0], array[1], array[2], array[3]};
        S_SetInitDic(a(), b(), c());
        _intLength = (byte) (rangeIsError ? 4 : 1);
        if (rangeIsError) {
            _id = 0;
        } else {
            //256-32-512-512
            _id = a() * 8388608 + b() * 262144 + c() * 512 + d();
            _idDic.put(_id, this);
        }
        _Dic.get(a()).get(b()).get(c()).put(d(), this);
    }

    public int o_id() {
        return _id;
    }

    int _id;

    public byte o_intLength() {
        return _intLength;
    }

    byte _intLength;

    public static CInt4 S_Register(String name, CInt4 onlyPath) {
        _nameDic.put(name, onlyPath);
        return onlyPath;
    }

    public static CInt4 Get(String name, Boolean isShowErrorLog) {
        if (_nameDic.containsKey(name)) {
            return _nameDic.get(name);
        }
        String[] sss0 = name.split("[,，_ ]");
        if (sss0.length < 4) {
            return CInt4.zero;
        }
        try {
            int aa = Integer.parseInt(sss0[0]);
            int bb = Integer.parseInt(sss0[1]);
            int cc = Integer.parseInt(sss0[2]);
            int dd = Integer.parseInt(sss0[3]);
            CInt4 onlyPath = CInt4.Get(aa, bb, cc, dd, isShowErrorLog);
            _nameDic.put(name, onlyPath);
            return onlyPath;
        } catch (Exception ex) {
            return CInt4.zero;
        }
    }

    public static CInt4 Get(int id, Boolean isShowErrorLog) {
        if (_idDic.containsKey(id)) {
            return _idDic.get(id);
        }
        int aa = id / 8388608;
        int bb = id % 8388608 / 262144;
        int cc = id % 262144 / 512;
        int dd = id % 512;
        if (id < 0) {
            aa = aa - 1;
            bb = 32 + bb - 1;
            cc = 512 + cc - 1;
            dd = 512 + dd;
        }
        return CInt4.Get(new int[]{aa, bb, cc, dd}, isShowErrorLog);
    }

    public static CInt4 Get(int aa, int bb, int cc, int dd) {
        return Get(aa, bb, cc, dd, false);
    }

    public static CInt4 Get(int aa, int bb, int cc, int dd, boolean isShowErrorLog) {
        return CInt4.Get(new int[]{aa, bb, cc, dd}, isShowErrorLog);
    }

    public static CInt4 Get(int[] array, boolean isShowErrorLog) {
        if (array == null || array.length < 4) {
            if (isShowErrorLog) {
                CLog.ef("C_Int4转化出错____int[] int4长度不够_____.{0}", array.toString());
            }
            return CInt4.zero;
        }
        boolean rangeIsError = S_RangeIsError(array, isShowErrorLog);
        S_SetInitDic(array);
        if (_Dic.get(array[0]).get(array[1]).get(array[2]).containsKey(array[3]) == false) {
            new CInt4(array, rangeIsError);
        }
        return _Dic.get(array[0]).get(array[1]).get(array[2]).get(array[3]);
    }

    /// <summary>
    /// 范围是否错误
    /// </summary>
    static Boolean S_RangeIsError(int[] array, Boolean isShowErrorLog) {
        if (S_RangeIsError(array[0], -256, 256)) {
            if (isShowErrorLog) {
                CLog.ef("超级错误.___{0}.____.a.越界", array.toString());
            }
            return true;
        }
        if (S_RangeIsError(array[1], 0, 32)) {
            if (isShowErrorLog) {
                CLog.ef("超级错误.____{0}.____.b.越界", array.toString());
            }
            return true;
        }
        if (S_RangeIsError(array[2], 0, 512)) {
            if (isShowErrorLog) {
                CLog.ef("超级错误.____{0}.____.c.越界", array.toString());
            }
            return true;
        }
        if (S_RangeIsError(array[3], 0, 512)) {
            if (isShowErrorLog) {
                CLog.ef("超级错误.____{0}.____.d.越界", array.toString());
            }
            return true;
        }
        return false;
    }

    /// <summary>
    /// 范围是否错误
    /// </summary>
    static Boolean S_RangeIsError(int value, int min, int max) {
        if (value < min) {
            return true;
        } else if (value > max) {
            return true;
        }
        return false;
    }


    static void S_SetInitDic(int... array) {
        if (_Dic.containsKey(array[0]) == false) {
            _Dic.put(array[0], new HashMap<>());
        }
        if (_Dic.get(array[0]).containsKey(array[1]) == false) {
            _Dic.get(array[0]).put(array[1], new HashMap<>());
        }
        if (_Dic.get(array[0]).get(array[1]).containsKey(array[2]) == false) {
            _Dic.get(array[0]).get(array[1]).put(array[2], new HashMap<>());
        }
    }

    @Override
    public String toString() {
        if (_toString == null) {
            _toString = _array[0] + "_" + _array[1] + "_" + _array[2] + "_" + _array[3];
        }
        return _toString;
    }

    String _toString;
}


